package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.category.AdminCategorySimpleInfoVo;
import com.cskaoyan.bean.vo.category.AdminCategoryVo;
import com.cskaoyan.service.admin.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :rain
 * @description /list
 * @since 2022/06/26 22:18
 */
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    /**
     * 显示全部商品类目和相关产品信息
     *
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/27 14:09:42
     */
    @RequestMapping("list")
    public BaseRespVo list() {
        CommonData<AdminCategoryVo> data = adminCategoryService.queryAllList();

        return BaseRespVo.ok(data);
    }

    /**
     * 获得类目等级为1的类目简单信息
     *
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/27 16:33:34
     */
    @RequestMapping("l1")
    public BaseRespVo l1() {
        CommonData<AdminCategorySimpleInfoVo> data = adminCategoryService.queryL1CategorySimpleInfo();

        return BaseRespVo.ok(data);
    }

    /**
     * 新建一个商品类目
     *
     * @param marketCategory 市场分类
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/27 17:00:25
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketCategory marketCategory) {
        //类目名不能为空
        if (StringUtils.isEmpty(marketCategory.getName().trim())) {
            return BaseRespVo.error("类目名不能为空");
        }
        //类目名不能重复
        CommonData<AdminCategoryVo> adminCategoryVoCommonData = adminCategoryService.queryAllList();
        for (AdminCategoryVo adminCategoryVo : adminCategoryVoCommonData.getList()) {
            if (adminCategoryVo.getName().trim().equals(marketCategory.getName().trim())) {
                return BaseRespVo.error("类目名不能重复");
            }
        }
        //二级类目父类目不能为0
        if (marketCategory.getLevel().equals("L2" )&& marketCategory.getPid().equals(0)) {
            return BaseRespVo.error("二级类目父类目不能为0");
        }
        MarketCategory data = adminCategoryService.create(marketCategory);

        return BaseRespVo.ok(data);
    }

    /**
     * 更新商品类别
     * 原版更新类别存在修改一级类目为二级类目时，若父类目选择原类目，会将该类目和类目下的子类目一起删除
     *
     * @param adminCategoryVo
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/27 17:30:47
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody AdminCategoryVo adminCategoryVo) {
        //数据校验，id和父类目id一致就返回错误信息
        if (adminCategoryVo.getId().equals(adminCategoryVo.getPid()) ) {
            return BaseRespVo.error("不能选择本身作为本身的上级类目");
        }
        //类目名不能为空
        if (StringUtils.isEmpty(adminCategoryVo.getName().trim())) {
            return BaseRespVo.error("类目名不能为空");
        }
        //类目名不能重复
        CommonData<AdminCategoryVo> adminCategoryVoCommonData = adminCategoryService.queryAllList();
        for (AdminCategoryVo adminCategory : adminCategoryVoCommonData.getList()) {
            if (adminCategory.getId().equals(adminCategoryVo.getId())) {
                continue;
            }else {
                if (adminCategory.getName().trim().equals(adminCategoryVo.getName().trim())) {
                    return BaseRespVo.error("类目名不能重复");
                }
            }
        }
        //二级类目父类目不能为0
        if (adminCategoryVo.getLevel().equals("L2" )&& adminCategoryVo.getPid().equals(0)) {
            return BaseRespVo.error("二级类目父类目不能为0");
        }
        //如果有子类目，一级类目不能变更二级类目
        if (adminCategoryVo.getLevel().equals("L2") && adminCategoryVo.getChildren() != null && adminCategoryVo.getChildren().size() != 0) {
            return BaseRespVo.error("该类目含有二级类目，请先处理该类目的二级类目");
        }

        adminCategoryService.update(adminCategoryVo);
        return BaseRespVo.ok();
    }

    /**
     * 删除对应类目和子类目下的类目
     *
     * @param marketCategory 市场分类
     * @return {@link BaseRespVo }
     * @author: jinmo
     * @date: 2022/06/27 18:22:27
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCategory marketCategory) {
        adminCategoryService.delete(marketCategory);

        return BaseRespVo.ok();
    }


    // @RequestMapping("list")
    // public BaseRespVo list() {
    //    CommonData<AdminCategoryVo> adminCategoryVo = adminCategoryService.queryAllList();
    //    return BaseRespVo.ok(adminCategoryVo);
    //
    // }

  /*  @RequestMapping("l1")
    public BaseRespVo l1() {
        adminCategoryService.queryAllL1();
    }*/
}
